package com.example.allplan.ShuJuKuCaoZuo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.allplan.JavaBean.JiHuaTiaoJavaBean;
import com.example.allplan.JavaBean.JiHuaXinXiJavaBean;
import com.example.allplan.JavaBean.YiGeChouQianXinXiJavaBean;
import com.example.allplan.JavaBean.YiGeQianJavaBean;
import com.example.allplan.SQhelper.YiGeChouQianSQhelper;
import com.example.allplan.SQhelper.YiGeJiHuaSQhelper;

import java.util.ArrayList;

public class YiGeChouQianSQCaoZuo {
    public String chou_qian_wen_jian_ming;
    public YiGeChouQianSQhelper mdbhelp;
    public SQLiteDatabase mDatabase;
    public Context mContext;

    public static ContentValues bean_to_values(YiGeQianJavaBean o) {
        ContentValues values = new ContentValues();
        values.put("id", o.getId());
        values.put("pai_xu_you_biao", o.getPai_xu_you_biao());
        values.put("chou_qian_ming", o.getChou_qian_ming());
        return values;
    }

    public static ContentValues xinxibean_to_value(YiGeChouQianXinXiJavaBean o) {
        ContentValues values = new ContentValues();
        values.put("id_zhi_zhen", o.getId_zhi_zhen());
        values.put("suo_shu_id", o.getSuo_shu_id());
        values.put("chou_qian_ming", o.getChou_qian_ming());
        values.put("wen_jian_ming", o.getWen_jian_ming());
        return values;
    }

    public static ContentValues xinxibean_to_value_only_three(YiGeChouQianXinXiJavaBean o) {
        ContentValues values = new ContentValues();
        values.put("id_zhi_zhen", o.getId_zhi_zhen());
        values.put("suo_shu_id",o.getSuo_shu_id());
        values.put("chou_qian_ming", o.getChou_qian_ming());
        return values;
    }

    public YiGeChouQianSQCaoZuo(Context mC, String ms) {
        chou_qian_wen_jian_ming = ms;
        mContext = mC;
        mdbhelp = new YiGeChouQianSQhelper(mContext, ms, null, 1);
    }

    public synchronized void openRead() {
        mDatabase = mdbhelp.getReadableDatabase();
    }

    public synchronized void close() {
        mDatabase.close();
    }

    public synchronized void xie_ru_xin_xi(YiGeChouQianXinXiJavaBean o) {
        openWrite();
        mDatabase.insert("yi_ge_xin_xi_biao", null, xinxibean_to_value(o));
        close();
    }

    public synchronized void geng_xin_xin_xi_only_three(YiGeChouQianXinXiJavaBean o) {
        openWrite();
        mDatabase.update("yi_ge_xin_xi_biao", xinxibean_to_value_only_three(o), "suo_shu_id=?", new String[]{"" + o.getSuo_shu_id()});
        close();
    }

    public synchronized YiGeChouQianXinXiJavaBean du_qu_xinxi() {
        openWrite();
        Cursor cursor = mDatabase.query("yi_ge_xin_xi_biao", new String[]{"id_zhi_zhen", "suo_shu_id", "chou_qian_ming", "wen_jian_ming"}, null, null, null, null, "id_zhi_zhen");
        cursor.moveToNext();
        return new YiGeChouQianXinXiJavaBean(cursor.getInt(cursor.getColumnIndex("id_zhi_zhen")), cursor.getInt(cursor.getColumnIndex("suo_shu_id")), cursor.getString(cursor.getColumnIndex("chou_qian_ming")), cursor.getString(cursor.getColumnIndex("wen_jian_ming")));
    }

    public synchronized void openWrite() {
        mDatabase = mdbhelp.getWritableDatabase();
    }

    public synchronized ArrayList<YiGeQianJavaBean> reloadAllChouQianTiao() {
        ArrayList<YiGeQianJavaBean> mList = new ArrayList<YiGeQianJavaBean>();
        openWrite();
        Cursor cursor = mDatabase.query("yi_ge_chou_qian_biao", new String[]{"id", "pai_xu_you_biao", "chou_qian_ming"}, null, null, null, null, "id");
        while (cursor.moveToNext()) {
            mList.add(new YiGeQianJavaBean(cursor.getInt(cursor.getColumnIndex("id")), cursor.getInt(cursor.getColumnIndex("pai_xu_you_biao")),0, cursor.getString(cursor.getColumnIndex("chou_qian_ming"))));
        }
        close();
        return mList;
    }

    public synchronized ArrayList<YiGeQianJavaBean> reloadAllChouQianTiaoWenHao() {
        ArrayList<YiGeQianJavaBean> mList = new ArrayList<YiGeQianJavaBean>();
        openWrite();
        Cursor cursor = mDatabase.query("yi_ge_chou_qian_biao", new String[]{"id", "pai_xu_you_biao", "chou_qian_ming"}, null, null, null, null, "id");
        while (cursor.moveToNext()) {
            mList.add(new YiGeQianJavaBean(cursor.getInt(cursor.getColumnIndex("id")), cursor.getInt(cursor.getColumnIndex("pai_xu_you_biao")),1, cursor.getString(cursor.getColumnIndex("chou_qian_ming"))));
        }
        close();
        return mList;
    }
    public synchronized void write_shu_zu(ArrayList<YiGeQianJavaBean> ml) {
        openWrite();
        for (int i = 0; i < ml.size(); i++) {
            mDatabase.insert("yi_ge_chou_qian_biao", null, bean_to_values(ml.get(i)));
        }
        close();
    }

    public synchronized void write_one(YiGeQianJavaBean o) {
        openWrite();
        mDatabase.insert("yi_ge_chou_qian_biao", null, bean_to_values(o));
        close();
    }

    public synchronized void update_one(YiGeQianJavaBean o) {
        openWrite();
        mDatabase.update("yi_ge_chou_qian_biao", bean_to_values(o), "id=?", new String[]{o.getId() + ""});
        close();
    }



    public synchronized void delete_suo_you_chuang_jian() {
        openWrite();
        mDatabase.delete("yi_ge_chou_qian_biao", null, null);
        close();
    }
}
